package com.ljkj.zjf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ljkj.access.Tlog;
import com.ljkj.access.Tuser;
import com.ljkj.common.db.dao.ZjfBlessDAO;
import com.ljkj.common.db.domain.ZjfBlessingLanguage;
import com.ljkj.common.db.domain.ZjfLabel;
import com.ljkj.common.db.util.DateTime;
import com.ljkj.common.db.util.RandomCode;

public class ZjfBlessService {
	
	private ZjfBlessDAO zjfBlessDAO;
	private TLogService tLogService;
	private ZjfLabelService zjfLabelService;


	/**
	 * 获取祝福语
	 * @param params
	 * @param currentPage
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public String getZjfBlessByProperties(String params,String labelId,int currentPage,int pageSize,String sortField,String sortOrder){
		
		Class[] pojos = {ZjfBlessingLanguage.class};
		String[] propertyNames=null;
		Object[] values=null;
		if(labelId != null && !"".equals(labelId)){
			propertyNames = new String[]{"labelId"};
			values = new String[]{labelId};
		}
		String[] likePropertyNames = {"blessName"};
		Object[] likevalues= {params};
		String[] betweenPropertyNames=null;
		Object[][]  betweenvalues=null;
		String[] orderByPropertyNames=null;
		Object[] orderByValues=null;
		
		if(sortField != null){
			orderByPropertyNames = new String[]{sortField};
			orderByValues =  new String[]{sortOrder};
		}

		int count = this.zjfBlessDAO.getCount(pojos, propertyNames, values,likePropertyNames, likevalues, betweenPropertyNames,betweenvalues);
		this.zjfBlessDAO.setPageMaxCount(pageSize);
		List<ZjfBlessingLanguage> zjfBlessingLanguages = this.zjfBlessDAO.findKnowByProperties(pojos, propertyNames, values, likePropertyNames, likevalues, betweenPropertyNames, betweenvalues, orderByPropertyNames, orderByValues, currentPage);
		
		List<Object> entry_list = new ArrayList<Object>();
		//迭代遍历所有条目，放到map里
		for(ZjfBlessingLanguage zjfBlessingLanguage : zjfBlessingLanguages){
			
			Map<String,Object> entry_map = new HashMap<String,Object>();
			entry_map.put("blessId",zjfBlessingLanguage.getBlessId());   
			entry_map.put("blessName",zjfBlessingLanguage.getBlessName());
			entry_map.put("blessImg",zjfBlessingLanguage.getBlessImg());
			String labelId_temp = zjfBlessingLanguage.getLabelId();
			entry_map.put("labelId",labelId_temp);
			if(labelId_temp != null &&!"".equals(labelId_temp)){
				ZjfLabel zjfLabel = this.zjfLabelService.getZjfLabelById(labelId_temp);
				if(zjfLabel != null){
					entry_map.put("labelName",zjfLabel.getLabelName());
				}
			}
			entry_map.put("createTime",zjfBlessingLanguage.getCreateTime());
			entry_map.put("createUser", zjfBlessingLanguage.getCreateUser());
			entry_map.put("updateTime", zjfBlessingLanguage.getUpdateTime());
			entry_map.put("updateUser", zjfBlessingLanguage.getUpdateUser());
			entry_map.put("disabledState", zjfBlessingLanguage.getDisabledState());
			
			entry_list.add(entry_map);
		}
		
		HashMap<String,Object> result_map = new HashMap<String, Object>();
		result_map.put("data", entry_list);
		result_map.put("total", count);
		
        return JSON.toJSONString(result_map);
	}
	
	
	/**
	 * 新增或更新标签
	 * @param zjfGoodsClass
	 * @return
	 */
	public String addOrEditZjfBlessInfo(ZjfBlessingLanguage zjfBlessingLanguage,Tuser tuser){
		
		Tlog tlog = new Tlog();
		Map<String,String> result_map = new HashMap<String, String>();
		if(zjfBlessingLanguage.getBlessId() == null){
			tlog.setOpType(1);
			tlog.setContent("新增祝福语："+ zjfBlessingLanguage.getBlessName());
			zjfBlessingLanguage.setBlessId(RandomCode.makeCode());
			zjfBlessingLanguage.setCreateTime(DateTime.getDateTime());
			zjfBlessingLanguage.setCreateUser(tuser.getLoginName());
			if(this.saveNewZjfBless(zjfBlessingLanguage)){
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				result_map.put("err_msg", "新增成功。");
				result_map.put("err_code", "0");
			}else{
				result_map.put("err_msg", "新增失败。");
				result_map.put("err_code", "1");
			}
			
		}else{
			try{
				if(zjfBlessingLanguage.getDisabledState()!=null && zjfBlessingLanguage.getDisabledState() == 1){
					tlog.setOpType(0);
					tlog.setContent("祝福语状态："+zjfBlessingLanguage.getBlessName()+" 变为不可用");
				}else{
					tlog.setOpType(1);
					tlog.setContent("修改祝福语："+zjfBlessingLanguage.getBlessName());
				}
				zjfBlessingLanguage.setUpdateTime(DateTime.getDateTime());
				zjfBlessingLanguage.setUpdateUser(tuser.getLoginName());
				this.zjfBlessDAO.update(zjfBlessingLanguage);
				this.tLogService.addOrEditTlogInfo(tlog, tuser);
				
				result_map.put("err_msg", "更新成功。");
				result_map.put("err_code", "0");
			}catch(Exception ex){
				result_map.put("err_msg", "更新失败。");
				result_map.put("err_code", "1");
			}
		}
		return JSON.toJSONString(result_map);
	}
	
	public boolean saveNewZjfBless(ZjfBlessingLanguage zjfBlessingLanguage){
		try{
			this.zjfBlessDAO.save(zjfBlessingLanguage);
			return true;
		}catch(Exception ex){
			return false;
		}
	}
	
	public ZjfBlessingLanguage getZjfBlessById(String blessId){
		return this.zjfBlessDAO.findById(blessId);
	}

	
	
	//注入DAO
	public void settLogService(TLogService tLogService) {
		this.tLogService = tLogService;
	}
	public void setZjfBlessDAO(ZjfBlessDAO zjfBlessDAO) {
		this.zjfBlessDAO = zjfBlessDAO;
	}
	public void setZjfLabelService(ZjfLabelService zjfLabelService) {
		this.zjfLabelService = zjfLabelService;
	}
}
